import _ from 'underscore'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'

import TimeStore from 'lib/stores/time-store'
import {coreApi,sortMap,parseToChart} from '../apis/core-util'


export default class MainScope extends Scope {
  constructor(){
    super();
    this.$state = {
      chart0:{
        loading:true
      },
      chart1:{
        loading:true
      },
      chart2:{
        loading:true
      },
      chart3:{
        loading:true
      }
    }
    this.chart0Api = coreApi({
      limit:5,
      urlName:'interaction',
      classify:'devicetype',
      viewKey:'avg_exec',
      order:sortMap['avg_exec']
    },TimeStore.getValue);
    this.chart1Api = coreApi({
      limit:5,
      urlName:'httptransaction',
      classify:'devicetype',
      classifyKeyField:'time',
      classifyCountField:'num',
      viewKey:'avg_resp',
      order:sortMap['avg_resp']
    },TimeStore.getValue);
    this.chart2Api = coreApi({
      limit:5,
      urlName:'errorrate',
      classify:'devicetype',
      viewKey:'error_rate',
      order:sortMap['error_rate'],
      errorCode:0
    },TimeStore.getValue);
    this.chart3Api = coreApi({
      limit:5,
      urlName:'session',
      classify:'devicetype',
      viewKey:'session_count',
      order:sortMap['session_count']
    },TimeStore.getValue);
  }
  fetch(){
    var _this = this
    this.chart0Api.fetch().then(function(data,params){
      _this.$state.chart0 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart0 = {
        error:err
      }
    },function(params){
      _this.$state.chart0 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.chart1Api.fetch().then(function(data,params){
      _this.$state.chart1 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart1 = {
        error:err
      }
    },function(params){
      _this.$state.chart1 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.chart2Api.fetch().then(function(data,params){
      _this.$state.chart2 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart2 = {
        error:err
      }
    },function(params){
      _this.$state.chart2 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.chart3Api.fetch().then(function(data,params){
      _this.$state.chart3 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart3 = {
        error:err
      }
    },function(params){
      _this.$state.chart3 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
  }
  $settime(payload){
    this.fetch()
  }
  $load(){
    this.fetch()
  }
}
